<?php
namespace app\blog\controller\admin;

use app\common\controller\Back;

class Index extends Back
{
    private $pageSize = 20;

    protected function initialize()
    {
        parent::initialize();
    }
    
    public function index()
    {
        $args = [
            'where' => [],
            'join'  => [],
        ];
        //排序
        $sortOrder = $this->query['sortOrder'];
        if(!in_array($sortOrder,model('common/Sort','loglic')->detail())){
            $sortOrder = 'detail_id';
        }
        $args['order'] = [
            //'detail_top' => 'desc',
            $sortOrder => 'desc',
        ];
        //状态
        if($this->query['status']){
            if(in_array($this->query['status'],model('common/Attr','loglic')->statusKeys())){
                array_push($args['where'],['detail_status','eq',$this->query['status']]);
            }
        }
        //分类
        if($this->query['typeId']){
            array_push($args['join'],['type_map','type_map.type_mp_did = detail_id']);
            array_push($args['where'],['type_map.type_mp_tid','eq',$this->query['typeId']]);
        }
        //密码
        if(in_array($this->query['password'],['null','not null'])){
            array_push($args['where'],['detail_extend->password',$this->query['password']]);
        }
        //搜索
        if($this->query['searchText']){
            array_push($args['where'],['detail_name|detail_title|detail_excerpt','like','%'.$this->query['searchText'].'%']);
        }
        //查询数据
        $pageList = model('blog/Index','loglic')->allPage($args,$this->pageSize,$this->site['page'],false,xfUrlPath('blog/admin.index/index',$this->query));
        //模板变量赋值
        $this->assign($pageList);
        $this->assign($this->site);
        $this->assign('query',$this->query);
        $this->assign('dataType',model('blog/Type','loglic')->listOptionLevel(0));
        //记录请求
        redirect()->remember();
        //渲染模板输出
        return $this->fetch();
    }

    public function create()
    {
        $form = xfFormCreate([
            'name'         => 'blogAdminIndexCreate',
            'action'       => xfUrl('blog/admin.index/save'),
            'reset'        => '重置',
            'class'        => 'row',
            'class_button' => 'text-center py-2',
            'items'        => model('blog/Index','form')->items([
                'detail_user' => $this->user['user_id'],
                'detail_slide' => $this->site['pathStatic'].'/images/x.jpg',
                'detail_cover' => $this->site['pathStatic'].'/images/y.jpg',
            ]),
        ]);
        $this->assign('form',$form);
        $this->assign($this->site);
        return $this->fetch();
    }
    public function save()
    {
        $post = input('post.');
        $model = model('blog/Index','loglic');
        $result = $model->dataSave($post);
        if(!$result){
            $this->error($model->getError());
        }
        $this->success('成功','blog/admin.index/index');
    }

    public function delete()
    {
        $result = model('common/Detail','loglic')->deleteId(input('id/d',0));
        if($result){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }

    public function deletes()
    {
        foreach(input('ids/a') as $key=>$id){
            model('common/Detail','loglic')->deleteId($id);
        }
        $this->success('成功');
    }

    public function edit()
    {
        if(!$id = input('id/d',0)){
            $this->error($this->error,'blog/admin.index/index');
        }
        //查询数据
        $result = model('blog/Index','loglic')->getId($id);
        if(!$result){
            $this->error($this->error,'blog/admin.index/index');
        }
        $form = xfFormCreate([
            'name'         => 'blogAdminIndexEdit',
            'action'       => xfUrl('blog/admin.index/update'),
            'reset'        => '重置',
            'class'        => 'row',
            'class_button' => 'text-center py-2',
            'items'        => model('blog/Index','form')->items($result),
        ]);
        $this->assign('form',$form);
        $this->assign($this->site);
        return $this->fetch();
    }
    public function update()
    {
        if(!$post = input('post.')){
            $this->error('参数错误');
        }
        $model = model('blog/Index','loglic');
        $result = $model->dataUpdate($post);
        if(!$result){
            $this->error($model->getError());
        }
        $this->success('成功',input('session.redirect_url'));
    }

    //批量审核
    public function normal()
    {
        $result = model('common/Detail','loglic')->fieldUpdate(input('ids/a'),'detail_status','normal');

        $this->success('成功');
    }

    //批量隐藏
    public function hidden()
    {
        $result = model('common/Detail','loglic')->fieldUpdate(input('ids/a'),'detail_status','hidden');

        $this->success('成功');
    }
}